package ru.spb.osll.myfit.client.widget;

import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.IncludeError;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageError;
import ru.spb.osll.myfit.common.SetUp;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TearDown;
import ru.spb.osll.myfit.common.WikiText;
import ru.spb.osll.myfit.common.PageElementType;

public class PageElementWidgetFactory {
	private GWTWidgetFactory m_gwtWidgetFactory;

	public PageElementWidgetFactory(GWTWidgetFactory gwtWidgetFactory) {
		m_gwtWidgetFactory = gwtWidgetFactory;
	}

	public PageElementWidget getPageElementWidget(PageElement pageElement) {
	  PageElementType type = pageElement.getType();
    switch (type){
      case INCLUDE_ERROR:
        return createIncludeExceptionWidget(m_gwtWidgetFactory, pageElement);
      case INCLUDE:
        return new IncludeWidget(m_gwtWidgetFactory, (Include) pageElement);
      case TABLE:
        return createTypedTable(m_gwtWidgetFactory, (Table) pageElement);
      case PAGE_ERROR:
        return new PageErrorWidget(m_gwtWidgetFactory, (PageError) pageElement);
      case WIKI_TEXT:
        return new WikiTextWidget(m_gwtWidgetFactory, (WikiText)pageElement);
      case SET_UP:        
        return new SetUpWidget(m_gwtWidgetFactory, (SetUp)pageElement);
      case TEAR_DOWN:
        return new SetUpWidget(m_gwtWidgetFactory, (TearDown)pageElement);
      default: 
        return null;
    }
//	  if (pageElement instanceof IncludeError) {
//      return createIncludeExceptionWidget(m_gwtWidgetFactory, pageElement);
//    } else if (pageElement instanceof Include) {
//			return new IncludeWidget(m_gwtWidgetFactory, (Include) pageElement);
//		} else if (pageElement instanceof Table) {
//			return createTypedTable(m_gwtWidgetFactory, (Table) pageElement);
//		} else if (pageElement instanceof PageError) {
//      return new PageErrorWidget(m_gwtWidgetFactory, (PageError) pageElement);
//    } else if(pageElement instanceof WikiText){
//      return new WikiTextWidget(m_gwtWidgetFactory, (WikiText)pageElement);
//    }else{
//	    return null;
//	  }
	}

	public IncludeWidget createIncludeExceptionWidget(GWTWidgetFactory gwtWidgetFactory, PageElement pageElement) {
	  IncludeWidget widget = new IncludeWidget(m_gwtWidgetFactory, (IncludeError) pageElement);
	  widget.setStyle();
	  return widget;
	}
	
	public TableWidget createTypedTable(GWTWidgetFactory gwtWidgetFactory,
			Table table) {
		TableWidget tableWidget;

		switch (table.getStyle()) {
		case DO_FIXTURE:
			tableWidget = new FlexTableWidget(gwtWidgetFactory, table);
			break;

		case SETUP_FIXTURE:
			tableWidget = new FixedTableWidget(gwtWidgetFactory, table);
			break;
			
		case COLUMN_FIXTURE:
			tableWidget = new FixedTableWidget(gwtWidgetFactory, table);
			break;

		case ONE_LINE_FIXTIRE:
			tableWidget = new OneLineTableWidget(gwtWidgetFactory, table);
			break;

		case MYFIT_TABLE_FIXTURE:
			tableWidget = new MyFitTableWidget(gwtWidgetFactory, table);
			break;

		default:
			// for demo
			tableWidget = new MyFitTableWidget(gwtWidgetFactory, table);
		}

		return tableWidget;
	}
}
